package General;

public class Square {
	private Tile myTile;			//Tile attached to Square
	
	private boolean hasVillager;	//true if villager attached
	private boolean hasDwarf;		//true if dwarf attached
		//Dwarfs add +2 gold if attached to a gold producing tile
	private Terrain validTerrain;	//which terrain it attaches to
	
	public Square(Terrain ter){
		validTerrain = ter;
		hasVillager = false;
		hasDwarf = false;
	}
	public Terrain getTerrain(){
		return validTerrain;
	}
	public Resource getYield(){
		//TODO calculate resources generated by this square
		if(hasTile()){
			Resource res = myTile.getYield();
			if(hasVillager){
				switch(myTile.getType()){
				case FOOD: 
					res = res.addResource(new Resource(1,0,0,0,0));
					break;
				case WOOD:
					res = res.addResource(new Resource(0,1,0,0,0));
					break;
				case GOLD:
					res = res.addResource(new Resource(0,0,1,0,0));
					break;
				case FAVOR: res = res.addResource(new Resource(0,0,0,1,0));
				}
			}
			if(hasDwarf){
				if(res.getGold()>0) res = res.addResource(new Resource(0,0,0,2,0));
			}
			return res;
		}else return new Resource();
	}
	public boolean attachVillager(){
		//return false if the tile already has a villager/dwarf
		//TODO smooth this feature out
		if (hasDwarf||hasVillager) return false;
		else {hasVillager=true; return true;}
	}
	public boolean attachDwarf(){
		//return false if already has a villager/dwarf
		//TODO smooth this feature out
		if (hasDwarf||hasVillager) return false;
		else {hasDwarf = true; return true;}
	}
	public void removeVillager(){
		hasVillager = false;
	}
	public void removeDwarf(){
		hasDwarf = false;
	}
	public boolean hasVillager(){
		return hasVillager;
	}
	public boolean hasDwarf(){
		return hasDwarf;
	}
	public boolean attachTile(Tile tile){
		if(!hasTile()){
			if(tile.getTerrain().equals(validTerrain)){
				myTile=tile;
				return true;
			}
		}
		return false;
	}
	public boolean hasTile(){
		return myTile!=null;
	}
	public Resources getTileType(){
		if(!hasTile()) return null;
		else return myTile.getType();
	}
	@Override
	public String toString(){
		String out = "";
		//out = super.toString();
		if(myTile!=null){
			out+=myTile.getType().toString()+ myTile.getQty()+" ";
		} else{ out+="----";}
		out+= validTerrain.toString();
		out+=" Vgr: "+ (hasVillager()?"Y":"N");
		out+=" Dwf: "+ (hasDwarf()?"Y":"N");
		return out;
	}
}
